import { useRequest } from 'umi';
import { getLevelZero, requestFunComponFiex, getUserID, getPosIDs, getUserDepID, getCompanyID } from '@/utils';
import { getBaseUrl } from '@/services/urlconst';
import { ExpertsTabSwitch } from '../mconfig/ManageTabSwitch';
import { useState } from 'react';

const baseUrl = getBaseUrl();
export const ExpertsManageService = () => {
  /**
   * 保存一遍搜索的参数
   * */
  const [searchParams, setSearchParams] = useState({});

  /**
   * 管理页列表接口
   * */
  const getExpertsManageService = (curRes) => {
    const request = useRequest(
      (requestParams?: Record<string, any>) => {
        let params: any = {
          createCompanyID: getLevelZero(),
          current: 1,
          pageSize: 10,
          sort: 'createTime',
          order: 'desc',

          ...requestParams,
          ...ExpertsTabSwitch(curRes),
        };

        /**处理作废时间*/
        if (params?.cancelTime) {
          const [cancelTimeStart, cancelTimeEnd] = params.cancelTime;
          params['cancelTimeStart'] = cancelTimeStart.format('YYYY-MM-DD 00:00:00');
          params['cancelTimeEnd'] = cancelTimeEnd.format('YYYY-MM-DD 23:59:59');
          delete params['cancelTime'];
        }

        /**区域处理*/
        if (params?.belongArea) {
          params['provinceCode'] = params.belongArea[0];
          params['cityCode'] = params.belongArea[1];
          params['countyCode'] = params.belongArea[2];
          delete params['belongArea'];
        }

        /**创建人部门处理*/
        if (params?.createFormNameDept || params?.createFormNameUser) {
          delete params['createFormNameDept'];
          delete params['createFormNameUser'];
          if (params?.createrID && params?.createrID?.length > 0) {
            params['createrID'] = params?.createrID?.toString();
          } else {
            delete params['createrID'];
          }
        }

        /**处理创建时间*/
        if (params?.createTime) {
          const [createTimeStart, createTimeEnd] = params.createTime;
          params['createTimeStart'] = createTimeStart.format('YYYY-MM-DD 00:00:00');
          params['createTimeEnd'] = createTimeEnd.format('YYYY-MM-DD 23:59:59');
          delete params['createTime'];
        }

        /** 处理搜索数据格式 */
        // overseaType | talentType | industryType
        params.industryType = params?.industryType ? params?.industryType?.toString(',') : null;
        params.overseaType = params?.overseaType ? params?.overseaType?.toString(',') : null;
        params.talentType = params?.talentType ? params?.talentType?.toString(',') : null;
        params.rankType = params?.rankType ? params?.rankType?.toString(',') : null;

        const reqUrl = `${baseUrl}/expertInfoController/dataGrid/${params?.params}`;
        setSearchParams(params);
        return requestFunComponFiex('POST', reqUrl, params);
      },
      {
        manual: false,
        paginated: true,
        defaultPageSize: 10,
      },
    );
    return {
      expertsManageRun: (params: any) => request.run(params),
      expertsManageDataSource: request.data?.rows || [],
      expertsManageLoading: request.loading,
      expertsManageTableProps: request.tableProps,
    };
  };

  /**
   * 取消专家
   * */
  const cancelExpertsService = () => {
    const request = useRequest(
      (payload: any) => {
        const reqUrl = `${baseUrl}/expertInfoController/cancelExpert`;
        let params = {
          cancelerID: getUserID(),
          cancelPositionID: getPosIDs(),
          cancelDepartmentID: getUserDepID(),
          cancelCompanyID: getCompanyID(),
          ...payload,
        };
        return requestFunComponFiex('PUT', reqUrl, params);
      },
      {
        manual: true,
      },
    );

    return {
      cancelExpertsRun: request.run,
      cancelExpertsLoading: request.loading,
    };
  };

  return {
    getExpertsManageService,
    cancelExpertsService,
    exportSearchParams: searchParams,
  };
};
